package elephant.servers.gameserver.codec;

import java.util.List;

import com.alibaba.fastjson.JSON;

/**
 * 
 * @author skydu
 *
 */
public class GameServerBodyCodec {

	public byte[] encode(short bodySerializeType,Object body){
		if(bodySerializeType==GameServerMessage.SERIALIZE_TYPE_JSON){
			return jsonEncode(body);
		}
		return null;
	}
	
	private byte[] jsonEncode(Object body) {
		String json=JSON.toJSONString(body)+"\r\n";
		return json.getBytes();
	}

	public List<String> decode(short bodySerializeType,String body){
		if(body==null||body.isEmpty()){
			return null;
		}
		if(bodySerializeType==GameServerMessage.SERIALIZE_TYPE_JSON){
			return jsonDecode(body);
		}
		return null;
	}

	private List<String> jsonDecode(String bodyJson) {
		return JSON.parseArray(bodyJson,String.class);
	}
}
